package net.crazyblock.mobilesafe;

import net.crazyblock.mobilesafe.utils.Md5Utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.opengl.ETC1Util;
import android.opengl.ETC1Util.ETC1Texture;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity {
	private GridView gv_list;
	private SharedPreferences sp;

	private static String[] list_name = { "手机防盗", "通讯卫士", "软件管理", "进程管理",
			"流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" };
	private static int[] list_icon_ids = { R.drawable.safe,
			R.drawable.callmsgsafe, R.drawable.app, R.drawable.taskmanager,
			R.drawable.netmanager, R.drawable.trojan, R.drawable.sysoptimize,
			R.drawable.atools, R.drawable.settings };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		gv_list = (GridView) findViewById(R.id.gv_list_home);
		gv_list.setAdapter(new MyGridViewAdapter());
		sp = getSharedPreferences("config", MODE_PRIVATE);

		gv_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent;
				switch (position) {
				// 进入手机防盗页面
				case 0:
					showLostAndFindDialog();
					// intent = new Intent(HomeActivity.this, cls);
					break;
				case 1:

					break;
				case 2:
					intent = new Intent(HomeActivity.this,AppManagerActivity.class);
					startActivity(intent);
					break;
				case 3:
					intent = new Intent(HomeActivity.this,TaskManagerActivity.class);
					startActivity(intent);
					break;
				case 4:

					break;
				case 5:

					break;
				case 6:

					break;
				case 7:

					break;
				// 进入设置中心
				case 8:
					intent = new Intent(HomeActivity.this,
							SettingActivity.class);
					startActivity(intent);

					break;
				}
			}
		});

	}

	protected void showLostAndFindDialog() {
		// TODO Auto-generated method stub
		if (isSetupPwd()) {
			// 已设置密码，弹出登陆对话框
			showSignInDialog();
		} else {
			// 未设置密码，弹出设置密码对话框
			showSetPwdDialog();
		}
	}

	private EditText et_input_pwd;
	private EditText et_confirm_pwd;
	private Button bt_confirm;
	private Button bt_cancle;
	private AlertDialog dialog;

	private void showSetPwdDialog() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new Builder(HomeActivity.this);
		View view = View.inflate(HomeActivity.this,
				R.layout.dialog_setup_password, null);
		et_input_pwd = (EditText) view.findViewById(R.id.et_input_pwd);
		et_confirm_pwd = (EditText) view.findViewById(R.id.et_confirm_pwd);
		bt_confirm = (Button) view.findViewById(R.id.bt_confirm);

		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();

		bt_cancle = (Button) view.findViewById(R.id.bt_cancle);
		bt_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		bt_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String input_pwd = et_input_pwd.getText().toString().trim();
				String confirm_pwd = et_confirm_pwd.getText().toString().trim();
				if (TextUtils.isEmpty(input_pwd)
						|| TextUtils.isEmpty(confirm_pwd)) {
					Toast.makeText(HomeActivity.this, "密码不能为空", 0).show();
					return;
				}

				if (input_pwd.equals(confirm_pwd)) {
					Editor editor = sp.edit();
					editor.putString("password",
							Md5Utils.md5Password(input_pwd));
					editor.commit();
					dialog.dismiss();
					// 进入手机防盗页面

				} else {
					Toast.makeText(HomeActivity.this, "所输入的密码不一样", 0).show();
					return;
				}

			}
		});

	}

	private void showSignInDialog() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new Builder(HomeActivity.this);
		View view = View.inflate(HomeActivity.this,
				R.layout.dialog_sign_in_password, null);

		dialog = builder.create();
		dialog.setView(view, 0, 0, 0, 0);
		dialog.show();

		bt_cancle = (Button) view.findViewById(R.id.bt_cancle);
		bt_confirm = (Button) view.findViewById(R.id.bt_confirm);
		et_input_pwd = (EditText) view.findViewById(R.id.et_input_pwd);

		bt_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		bt_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String input_pwd = et_input_pwd.getText().toString().trim();
				String pwd = sp.getString("password", null);
				if (Md5Utils.md5Password(input_pwd).equals(pwd)) {
					// 进入防盗页面
					Intent intent = new Intent(HomeActivity.this,
							LostFindActivity.class);
					startActivity(intent);
					dialog.dismiss();
				} else {
					Toast.makeText(HomeActivity.this, "密码不正确", 0).show();
					et_input_pwd.setText("");
				}
			}
		});
	}

	/**
	 * 判断手机防盗是否设置了密码
	 * 
	 * @return true 已设置密码 ；false 未设置密码
	 */
	private boolean isSetupPwd() {
		String result = sp.getString("password", null);
		return !TextUtils.isEmpty(result);

	}

	private class MyGridViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list_name.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = View.inflate(HomeActivity.this,
					R.layout.gridview_item_list_home, null);
			;

			TextView tv = (TextView) view.findViewById(R.id.tv_item_name);
			ImageView iv = (ImageView) view.findViewById(R.id.iv_item_icon);
			tv.setText(list_name[position]);
			iv.setBackgroundResource(list_icon_ids[position]);
			return view;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

	}
}
